package com.yz.xyt.controller;

import com.github.pagehelper.PageInfo;
import com.yz.xyt.constant.Constants;
import com.yz.xyt.constant.HttpStatus;
import com.yz.xyt.domain.ResponseResult;
import com.yz.xyt.domain.Student;
import com.yz.xyt.domain.vo.StudentVo;
import com.yz.xyt.service.IBaseDataImportService;
import com.yz.xyt.service.IRoleService;
import com.yz.xyt.service.IStudentService;
import com.yz.xyt.service.IUserService;
import com.yz.xyt.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 *学生账号信息管理
 * @author JW.WANG
 */
@RestController
@RequestMapping("/system")
public class StudentManagerController {

    @Autowired
    IStudentService studentService;

    @Autowired
    IBaseDataImportService baseDataImportService;

    @Autowired
    IUserService userService;

    @Autowired
    IRoleService roleService;

    /**
     * 分页查询学生信息
     * @param studentVo
     * @return
     */
    @RequestMapping("/getStudentList")
    public ResponseResult getStudentList(StudentVo studentVo){
        ResponseResult result = new ResponseResult();
        List<String> roles = SecurityUtils.getLoginUser().getRoles();
        PageInfo studentPage;
        if(roles.contains(Constants.APPROVAL_ROLE_SJ)){
            studentVo.setStaffNumber(SecurityUtils.getUsername());
            studentPage= studentService.findBySjForPage(studentVo);
        }else if(roles.contains(Constants.APPROVAL_ROLE_FD)){
            studentVo.setStaffNumber(SecurityUtils.getUsername());
            studentPage= studentService.findStudentForPage(studentVo);
        }else{
            studentPage= studentService.findStudentForPage(studentVo);
        }

        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("查询成功");
        result.setData(studentPage);
        return result;
    }

    /**
     * 学生数据导入
     * @param file
     * @return
     */
    @RequestMapping("/importStudent")
    public ResponseResult importStudent(MultipartFile file){
        ResponseResult result = baseDataImportService.importStudent(file);
        return result;
    }

    /**
     * 查询学生列表
     * @param studentVo
     * @return
     */
    @RequestMapping("/findStudentList")
    public ResponseResult findStudentList(StudentVo studentVo){
        ResponseResult result =  new ResponseResult();

        try {
           result =  studentService.findStudentList(studentVo);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("查询学生列表失败");
            result.setCode(HttpStatus.ERROR);
        }

        return result;
    }

    /**
     * 增加学生
     */
    @RequestMapping("/insertStudent")
    public ResponseResult addStudent(Student student){
        ResponseResult result =  new ResponseResult();
        try {
            result = studentService.addStudent(student);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(HttpStatus.ERROR);
            result.setMsg("新增学生失败");
        }
        return result;
    }


    /**
     * 修改学生信息
     */
    @RequestMapping("/updateStudent")
    public ResponseResult updateStudent(Student student){
        ResponseResult result =  new ResponseResult();
        try {
            result = studentService.updateStudent(student);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(HttpStatus.ERROR);
            result.setMsg("修改学生失败");
        }
        return result;
    }


    /**
     * 删除学生
     */
    @RequestMapping("/deleteStudentByStudentNumber")
    public ResponseResult deleteStudentByStudentNumber(Student student){
        ResponseResult result =  new ResponseResult();
        try {
            result = studentService.deleteStudent(student);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(HttpStatus.ERROR);
            result.setMsg("删除学生失败");
        }
        return result;
    }

    /**
     * 查询学生信息
     * @param studentVo
     * @return
     */
    @RequestMapping("/findStudentInfo")
    public ResponseResult findStudentInfo(StudentVo studentVo){
        ResponseResult result = new ResponseResult();
        List<StudentVo> studentVoList = studentService.getStudents(studentVo);
        result.setMsg("查询学生信息成功");
        result.setCode(HttpStatus.SUCCESS);
        result.setData(studentVoList.get(0));
        return result;
    }
}
